package cn.com.dragonpass.categoryagsvc.web.consumer.utils;

import cn.com.dragonpass.common.utils.StringUtils;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.util.Arrays;
import java.util.stream.Collectors;

public class JSONObjectUtils {

    public static String jsonObjectValueToString(String string) {
        if (StringUtils.isNotBlank(string)) {
            string = string.replace("{", "").replace("}", "").replaceAll("\"", "");
            String[] split = string.split(",");
            if (split.length > 0) {
                return Arrays.stream(split).map(m -> {
                    String[] split1 = m.split(":");
                    if (split1.length > 0) {
                        return split1[1];
                    }
                    return "";
                }).collect(Collectors.joining(","));
            }
        }
        return null;
    }

    /**
     * 合并JSON对象，用source覆盖target，返回覆盖后的JSON对象，
     *
     * @param source JSONObject
     * @param target JSONObject
     * @return JSONObject
     */
    public static JSONObject jsonMerge(JSONObject source, JSONObject target) {
        // 覆盖目标JSON为空，直接返回覆盖源
        if (target == null) {
            return source;
        }

        for (String key : source.keySet()) {
            Object value = source.get(key);
            if (!target.containsKey(key)) {
                target.put(key, value);
            } else {
                if (value instanceof JSONObject) {
                    JSONObject valueJson = (JSONObject) value;
                    JSONObject targetValue = jsonMerge(valueJson, target.getJSONObject(key));
                    target.put(key, targetValue);
                } else if (value instanceof JSONArray) {
                    JSONArray valueArray = (JSONArray) value;
                    for (int i = 0; i < valueArray.size(); i++) {
                        JSONObject obj = (JSONObject) valueArray.get(i);
                        JSONObject targetValue = jsonMerge(obj, (JSONObject) target.getJSONArray(key).get(i));
                        target.getJSONArray(key).set(i, targetValue);
                    }
                } else {
                    target.put(key, value);
                }
            }
        }
        return target;
    }
}


-------------------

  public static final String SETTLE_CUS = "settleCus";
 @Override
    public void aloneHandleSup(SaleOrderDto.CreateV2.Input salrOrder, ServiceOrderDTO.CreateV2.Input svcOrder) {
        String detail = svcOrder.getDetail();
        if (StringUtils.isEmpty(detail)) {
            return;
        }

        JSONObject json = JSON.parseObject(detail);
        OrderSettleMsgRv.SettleSup settleSup = JSON.parseObject(JSON.toJSONString(json.getJSONObject(AbstractOrderSettle.SETTLE_SUP)), OrderSettleMsgRv.SettleSup.class);

        //出发类型的转换
        String departureType = settleSup.getDepartureType();
        if (!StringUtils.isEmpty(departureType)) {
            String departureTypeName = super.getDepartureTypeName(departureType);
            settleSup.setDepartureType(departureTypeName);
        }

        json.put(SETTLE_CUS, JSON.parseObject(JSON.toJSONString(settleCus)));
        json.put(SETTLE_INFO, settleInfo);
        JSONObject merge = JSONObjectUtils.jsonMerge(json, JSON.parseObject(detail));

    }


public class OrderSettleMsgMg {


    @Data
    public static class SettleInfo{


        /**
         * 网点编号
         */
        private String networkCode;

        /**
         * 网点名称
         */
        private String networkName;

        /**
         * 站点编号
         */
        private String stationCode;

        /**
         * 站点名称
         */
        private String stationName;

        /**
         * 站点类型
         */
        private String stationType;


        /**
         * 服务级别
         */
        private String serviceLevel;




        /**
         * 下单时间
         */
        private  Date orderCreateTime;

        /**
         * 服务时间
         */
        private Date serviceTime;


        /**
         * 核销凭证号
         */
        private String voucher;


        /**
         * 结算标签
         */
        private String orderFlag;


    }


    /**
     * 客户相关信息
     */
    @Data
    public static class SettleCus{


        /**
         * 补充服务-说明
         */
        private String addedRemark;

        /**
         * 补充服务-金额
         */
        private BigDecimal addedAmount;


        /**
         * 服务人数
         */

        private Integer perPeopleNums=0;


        /**
         *儿童人数
         */
        private Integer childCount = 0;


        /**
         * 鲜花数量
         */
        private Integer flowers=0;


        /**
         * 鲜花费
         */
        private BigDecimal flowerFee=BigDecimal.ZERO;


        /**
         * 高铁接送的次数
         */
      //  private Integer transferNums=0;


        /**
         * 高铁单价
         */
       // private BigDecimal transferPrice;



        /**
         * 电瓶车数量
         */
        private  Integer batteryCarts;

        /**
         * 电瓶车费用
         */
        private BigDecimal batteryCartFee=BigDecimal.ZERO;


        /**
         * 是否高铁接送(0-否，1-是)
         */
        private  Integer isTransfer;


        /**
         * 摆渡车数量
         */
        private  Integer shuttleBusNums=0;


        /**
         * 摆渡车费用
         */
        private BigDecimal shuttleBusFee=BigDecimal.ZERO;

        /**
         * 行李重量
         */
        private  BigDecimal luggageWeight;


        /**
         * 陪同人数
         */
        private  Integer escorts=0;


        /**
         * 品类类型
         */
        private  String categoryType;


        /**
         * 备注
         */
        private String remark;



        /**
         * 结算方
         */
        private String settleParty;


        /**
         *币种
         */
        private String currency;


        /**
         * 计费方式
         */
        private String billingType;


        /**
         *单价
         */
        private BigDecimal unitPrice;


        //第三方订单号(客户)
    private  String cusClientOrderNo;





        //通过接口获取


        //儿童单价
        private BigDecimal childPrice=BigDecimal.ZERO;

        //免费儿童人数
        private BigDecimal childFeeNums=BigDecimal.ZERO;

        //鲜花单价
        private BigDecimal flowerPrice=BigDecimal.ZERO;


        //电瓶车单价
        private BigDecimal batteryCartPrice=BigDecimal.ZERO;



        //摆渡车单价
        private BigDecimal shuttleBusPrice=BigDecimal.ZERO;

        //行李费用
//        private BigDecimal luggageFee;

        //陪同单价
        private BigDecimal escortPrice=BigDecimal.ZERO;

        //
     private  BigDecimal settleAmount;


     //增加儿童费用
      private  BigDecimal childFee;


        //非标单的附加费
        private  BigDecimal  additionAmount;


       private BigDecimal batchNumber;//贵宾批次


        /**
         * 附加服务费
         */
        private List<OrderSettleMsg.AdditionPrice> additionPrice;


        /**
         * 总服务人数
         */
        private  Integer totalServiceNums=0;

        /**
         * 包房费用
         */
        private BigDecimal roomFee;

        /**
         * 大包房数量
         */
        private  Integer largeRoomNums=0;
        /**
         * 中包房数量
         */
        private  Integer middleRoomNums=0;

        /**
         * 小包房数量
         */
        private  Integer smallRoomNums=0;

        /**
         * 大包房单价
         */
        private BigDecimal largeRoomPrice;
        /**
         * 中包房单价
         */
        private BigDecimal middleRoomPrice;

        /**
         * 小包房单价
         */
        private BigDecimal smallRoomPrice;


        /**
         * 大包房可免人数
         */
        private BigDecimal largeRoomFeePeople=BigDecimal.ZERO;

        /**
         * 中包房可免人数
         */
        private BigDecimal middleRoomFeePeople=BigDecimal.ZERO;

        /**
         * 小包房可免人数
         */
        private BigDecimal smallRoomFeePeople=BigDecimal.ZERO;


        /**
         * 贵宾服务费
         */
        private BigDecimal guestServiceFee;
    }


    /**
     * 供应商信息
     */
    @Data
    public static class SettleSup{


        /**
         * 接送类型
         */
        private String pickupType;

        /**
         * 补充服务-说明
         */
        private String addedRemark;

        /**
         * 补充服务-金额
         */
        private BigDecimal addedAmount;

        /**
         * 总服务人数
         */
        private  Integer totalServiceNums=0;

        /**
         * 服务人数
         */

        private Integer perPeopleNums=0;


        /**
         *儿童人数
         */
        private Integer childCount = 0;

        //儿童单价
        private BigDecimal childPrice=BigDecimal.ZERO;;


        /**
         * 陪同人数
         */
        private  Integer escorts=0;



        /**
         * 备注
         */
        private String remark;


        /**
         * 第三方编号
         */
        private String supClientOrderNo;




        /**
         * 计费方式 供应商
         */
        private String billingType;



        /**
         *单价
         */
        private BigDecimal unitPrice;


        private  BigDecimal settleAmount;


        private String currency;

        //非标单的附加费
        private  BigDecimal  additionAmount;

        /**
         * 附加服务费
         */
        private List<OrderSettleMsg.AdditionPrice> additionPrice;


        private Double batchNumber;//贵宾批次--作为详情的数量（需求）


        /**
         * 夜间服务费
         */
        private BigDecimal nightPrice;


        /**
         * 陪同单价
         */
        private BigDecimal escortPrice;


        /**
         * 通道费
         */
        private BigDecimal channelFee;




        /**
         * 包房费用
         */
        private BigDecimal roomFee;

        /**
         * 大包房数量
         */
        private  Integer largeRoomNums=0;
        /**
         * 中包房数量
         */
        private  Integer middleRoomNums=0;

        /**
         * 小包房数量
         */
        private  Integer smallRoomNums=0;

        /**
         * 大包房单价
         */
        private BigDecimal largeRoomPrice=BigDecimal.ZERO;
        /**
         * 中包房单价
         */
        private BigDecimal middleRoomPrice=BigDecimal.ZERO;

        /**
         * 小包房单价
         */
        private BigDecimal smallRoomPrice=BigDecimal.ZERO;


        /**
         * 大包房可免人数
         */
        private Long largeRoomFeePeople=0l;

        /**
         * 中包房可免人数
         */
        private Long middleRoomFeePeople=0l;

        /**
         * 小包房可免人数
         */
        private Long smallRoomFeePeople=0l;

        private BigDecimal guestServiceFee;

    }
}    